/**
 * 
 */
package com.ideeli.webextractor.service;

import static com.ideeli.webextractor.utils.Constants.DEL_LIMIT;
import static com.ideeli.webextractor.utils.Constants.INGRAM_PASS;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.log4j.Logger;

import com.ideeli.webextractor.utils.Constants;

/**
 * @author sa2
 * 
 */
public class WebExtractInvokerImpl implements WebExtractInvoker {
	
	private static final Logger logger = Logger.getLogger(WebExtractInvokerImpl.class);

	private CacheService cacheService;

	private IngExtractorService ingExtractorFrontService;

	private IngExtractorService ingExtractorBackService;

	private ProductService productService;

	private ProductImageService productImageService;

	@Override
	public void invoke(String arg[]) throws Exception {

		cacheService.setAppProperty(INGRAM_PASS, arg[0]);
		cacheService.setAppProperty(DEL_LIMIT, arg[1]);
		cacheService.addConfigMap(Constants.LICENCE_PRODUCT_VAL, arg[3]);
		
		try{
			cacheService.setAppProperty(Constants.LIKE_CNT, arg[4]);
		}catch(Exception e){
			logger.info("Next time pass 4th parameter of like count");
		}

		ingExtractorFrontService.setThreadName("FRONT");
		ingExtractorBackService.setThreadName("BACK");

		long startTime = System.currentTimeMillis();
		logger.warn("startTime : " + startTime);

		ExecutorService exeService = Executors.newFixedThreadPool(2);

		List<Future<String>> futures = new ArrayList<Future<String>>();

		futures.add(exeService.submit(ingExtractorFrontService));
		futures.add(exeService.submit(ingExtractorBackService));

		// Following code waits untila all the submitted thread finishes at
		// future.get call
		for (Future<String> future : futures) {
			logger.warn("future.get():" + future.get());
		}

		exeService.shutdownNow();
		long endTime = System.currentTimeMillis();
		logger.warn("endTime : " + endTime);

		productService.deleteUnlisted(1);

		Map<String, String> updatedPriceList = cacheService
				.getUpdateProdPriceList();

		for (String productId : updatedPriceList.keySet()) {
			logger.warn("productId:" + productId + "  "
					+ updatedPriceList.get(productId));
		}

		productImageService.synchronizeImages();
		
		logger.info("creating featured products");
		productService.createFeaturedProdcuts();
		logger.info("creating featured products Finished");

		logger.info("\n\n\n");
		List<String> errorList = cacheService.getErrorList();

		for (String str : errorList) {
			logger.warn(str);
		}

		logger.warn("Extracting completed!!\n\n\n");

		logger.warn("Total time : " + ((endTime - startTime) / 1000) / 60
				+ " minutes");

	}
	
	public CacheService getCacheService()
	{
		return cacheService;
	}

	public void setCacheService(CacheService cacheService)
	{
		this.cacheService = cacheService;
	}

	public IngExtractorService getIngExtractorFrontService()
	{
		return ingExtractorFrontService;
	}

	public void setIngExtractorFrontService(
			IngExtractorService ingExtractorFrontService)
	{
		this.ingExtractorFrontService = ingExtractorFrontService;
	}

	public IngExtractorService getIngExtractorBackService()
	{
		return ingExtractorBackService;
	}

	public void setIngExtractorBackService(
			IngExtractorService ingExtractorBackService)
	{
		this.ingExtractorBackService = ingExtractorBackService;
	}

	public ProductService getProductService()
	{
		return productService;
	}

	public void setProductService(ProductService productService)
	{
		this.productService = productService;
	}

	public ProductImageService getProductImageService()
	{
		return productImageService;
	}

	public void setProductImageService(ProductImageService productImageService)
	{
		this.productImageService = productImageService;
	}


}
